#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sys
import json

sys.path.append(
    os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "qtrade")
)

import requests
from contextlib import contextmanager

# 测试
base_url = "http://localhost:8000/backtest/"

headers = {
}


#!/usr/bin/env python
# -*- coding:utf-8 -*-
from pyvirtualdisplay import Display
# # 要想调用键盘按键操作需要引入keys包
from selenium.webdriver.common.keys import Keys
# from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

# binary = FirefoxBinary('/usr/bin/geckodriver')
# driver = webdriver.Firefox(firefox_binary=binary)

# #创建浏览器对象
from selenium import webdriver
from selenium.webdriver import FirefoxOptions

@contextmanager
def firefox_brower():
    driver = None
    display = None
    try:
        # Set screen resolution to 1366 x 768 like most 15" laptops
        display = Display(visible=0, size=(1366, 768))
        display.start()
        opts = FirefoxOptions()
        opts.add_argument('–headless')
        driver = webdriver.Firefox(options=opts)
        driver.set_window_size(1366, 768)
        yield driver
    except Exception as e:
        raise e
    finally:
        if driver is not None:
            driver.quit()
        if display is not None:
            # quit Xvfb display
            display.stop()




def test_baidu():
    with firefox_brower() as driver:
        driver.get("https://www.baidu.com")
        #打印页面标题“百度一下你就知道”
        print(driver.title)
        #生成当前页面快照
        driver.save_screenshot("baidu.png")
        # id="kw"是百度搜索框，输入字符串“微博”，跳转到搜索中国页面
        driver.find_element_by_id("kw").send_keys(u"微博")
        # id="su"是百度搜索按钮，click() 是模拟点击
        driver.find_element_by_id("su").click()
        # 获取新的页面快照
        driver.save_screenshot(u"微博.png")
        # 打印网页渲染后的源代码
        print(driver.page_source)
        # 获取当前页面Cookie
        print(driver.get_cookies())
        # ctrl+a 全选输入框内容
        driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')
        # ctrl+x 剪切输入框内容
        driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x')
        # 输入框重新输入内容
        driver.find_element_by_id("kw").send_keys("test")
        # 模拟Enter回车键
        driver.find_element_by_id("su").send_keys(Keys.RETURN)
        # 清除输入框内容
        driver.find_element_by_id("kw").clear()
        # 生成新的页面快照
        driver.save_screenshot("test.png")
        # 获取当前url
        print(driver.current_url)



def test_eastmoney():
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    with firefox_brower() as driver:
        driver.get("http://quote.eastmoney.com/concept/sh600031.html")
        #打印页面标题“百度一下你就知道”
        print(driver.title)
        #生成当前页面快照
        # driver.save_screenshot("baidu.png")
        # id="kw"是百度搜索框，输入字符串“微博”，跳转到搜索中国页面
        driver.find_element_by_xpath('//*[@id="cmfb-btn"]').click()
        driver.save_screenshot("东方1.png")
        # aa = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[3]/div[1]/div[2]/div[3]/div[1]/div[2]/div[2]/div[1]/div[3]/div[2]/span')
        # aa = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[3]/div[1]/div[2]/div[3]/div[1]/div[2]/div[2]/div[1]/div[3]')
        bb = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[3]/div[1]/div[2]/div[3]/div[1]/div[2]/div[2]/div[1]/div[3]/div[2]')
        cc = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[3]/div[1]/div[2]/div[3]/div[1]/div[2]/div[2]/div[1]/div[3]/div[1]/label')
        dd = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[3]/div[1]/div[2]/div[3]/div[1]/div[2]/div[2]/div[1]/div[3]/div[2]/label')
        ee = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[3]/div[1]/div[2]/div[3]/div[1]/div[2]/div[2]/div[1]/div[3]/div[2]/span')
        bb = WebDriverWait(driver, 50).until(EC.presence_of_all_elements_located((By.XPATH, '/html/body/div[1]/div[1]/div[3]/div[1]/div[2]/div[3]/div[1]/div[2]/div[2]/div[1]/div[3]/div[2]/span')))
        # bb = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[3]/div[1]/div[2]/div[3]/div[1]/div[2]/div[2]/div[1]/div[3]/div[3]/span[0]')

        bb.text
        driver.find_element_by_id("kw").send_keys(u"微博")
        # id="su"是百度搜索按钮，click() 是模拟点击

        # 获取新的页面快照
        driver.save_screenshot(u"微博.png")
        # 打印网页渲染后的源代码
        print(driver.page_source)
        # 获取当前页面Cookie
        print(driver.get_cookies())
        # ctrl+a 全选输入框内容
        driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')
        # ctrl+x 剪切输入框内容
        driver.find_element_by_id("kw").send_keys(Keys.CONTROL,'x')
        # 输入框重新输入内容
        driver.find_element_by_id("kw").send_keys("test")
        # 模拟Enter回车键
        driver.find_element_by_id("su").send_keys(Keys.RETURN)
        # 清除输入框内容
        driver.find_element_by_id("kw").clear()
        # 生成新的页面快照
        driver.save_screenshot("test.png")
        # 获取当前url
        print(driver.current_url)

if __name__ == "__main__":
    test_eastmoney()
